﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.AI;
using UnityEngine.Profiling;
using UnityEngine.Rendering;

partial class CameraRenderer
{
    private Material errorMaterial;
    private static ShaderTagId[] legacyShaderTagIds =
    {
        new ShaderTagId("Always"),
        new ShaderTagId("ForwardBase"),
        new ShaderTagId("PrepassBass"),
        new ShaderTagId("Vertex"),
        new ShaderTagId("VertexLMRGBM"),
        new ShaderTagId("VertexLM")
    };

    partial void DrawGizmos();
    partial void DrawUnsupportedShaders();
    partial void PrepareForSecondWindow();
    partial void PrepareBuffer();

#if UNITY_EDITOR

    private string SampleName;

    partial void DrawGizmos()
    {
        if (Handles.ShouldRenderGizmos())
        {
            this.contex.DrawGizmos(this.camera, GizmoSubset.PreImageEffects);
            this.contex.DrawGizmos(this.camera, GizmoSubset.PostImageEffects);
        }
    }

    //绘制不支持的shader
    partial void DrawUnsupportedShaders()
    {
        if (errorMaterial == null)
        {
            //显示为洋红色错误材质
            errorMaterial = new Material(Shader.Find("Hidden/InternalErrorShader"));
        }

        var drawingSetting = new DrawingSettings(
            legacyShaderTagIds[0], new SortingSettings(this.camera)
        )
        {
            overrideMaterial = errorMaterial
        };

        for (int i = 1; i < legacyShaderTagIds.Length; i++)
        {
            drawingSetting.SetShaderPassName(i, legacyShaderTagIds[i]);
        }

        var filteringSetting = FilteringSettings.defaultValue;

        this.contex.DrawRenderers(cullingResults, ref drawingSetting, ref filteringSetting);
    }

    partial void PrepareForSecondWindow()
    {
        if (this.camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(this.camera);
        }
    }

    partial void PrepareBuffer()
    {
        Profiler.BeginSample("Editor Only");
        this.buffer.name = SampleName = this.camera.name;
        Profiler.EndSample();
    }

#else

    const string SampleName = bufferName;

#endif

}
